package cn.itsource.controller;

import cn.itsource.feign.LogisticFeignClient;
import cn.itsource.pojo.Logistic;
import cn.itsource.pojo.Order;
import cn.itsource.service.IOrderService;
import cn.itsource.util.AjaxResult;
import cn.itsource.util.JSONResult;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.sun.org.apache.bcel.internal.generic.IF_ACMPEQ;
import com.sun.org.apache.regexp.internal.RE;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private IOrderService orderService;
    @Autowired
    private LogisticFeignClient logisticFeignClient;

    @PutMapping("/{id}")
//    @HystrixCommand(fallbackMethod = "getLogisticFallback")
    public JSONResult receive(@PathVariable Integer id){
        List<Object> updateAllInfo = new ArrayList<>();
        Order order = orderService.queryById(id);
        String orderStatus = order.getOrderStatus();
        try {
            if (!orderStatus.equals("已收货")){
                order.setOrderStatus("已收货");
                orderService.update(order);
                updateAllInfo.add(order);

                Logistic logistic = logisticFeignClient.queryBySn(id);
                AjaxResult result = logisticFeignClient.saveOrUpdate(logistic);

                if(!result.getMessage().equals("操作失败")){
                    logistic.setLogisticsStatus("已送达");
                    logisticFeignClient.saveOrUpdate(logistic);
                }
                updateAllInfo.add(logistic);
            }else {
                updateAllInfo.add(order);
            }
//            orderService.queryAllInfo(id);
            return JSONResult.success(updateAllInfo,"200","订单已成功签收");
        } catch (Exception e) {
            e.printStackTrace();
            return JSONResult.error("订单编号不存在","400");
        }
    }

    @GetMapping("/logistic/{id}")
    public JSONResult queryAllInfo(@PathVariable("id") Integer id){
        List<Object> allInfo = new ArrayList<>();
        Order order = orderService.queryById(id);
        String orderStatus = order.getOrderStatus();
        try {
            if (orderStatus.equals("已发货")||orderStatus.equals("已收货")){
                allInfo.add(order);
                Logistic logistic = logisticFeignClient.queryBySn(id);
                allInfo.add(logistic);
            }else {
                allInfo.add(order);
            }
            orderService.queryAllInfo(id);
            return JSONResult.success(allInfo,"200","查询成功");
        } catch (Exception e) {
            e.printStackTrace();
            return JSONResult.error("订单编号不存在","400");
        }
    }

//    @GetMapping("/{oid}")
//    public Logistic queryBySn(@PathVariable Integer oid){
//        return logisticFeignClient.queryBySn(oid);
//    }

    @GetMapping("/query/all")
    public List<Order> queryAll(){
        List<Order> orderList = orderService.queryAll();
        for (Order order : orderList) {
            System.out.println(order);
        }
        return orderList;
    }

    @GetMapping("/{id}")
    public Order queryById(@PathVariable("id") Integer id){
        return orderService.queryById(id);
    }

    @PutMapping("/saveOrUpdate")
    public AjaxResult saveOrUpdate(@RequestBody Order order){
        try {
            if(order.getId() == null){
                orderService.add(order);
            }else {
                orderService.update(order);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("操作失败");
        }
    }

//    public JSONResult getLogisticFallback(@PathVariable("id") Integer id){
//        List<Object> fallbackList = new ArrayList<>();
//
//        Order order = new Order();
//        order.setId(123456);
//        order.setOrderStatus("待发货");
//        order.setCreateTime(new Date());
//        order.setDeliveryTime(null);
//        order.setReceiveTime(null);
//        Logistic logistic = new Logistic();
//        logistic.setId(-1);
//        logistic.setOid(123456);
//        logistic.setLogisticsStatus("暂无物流信息");
//        logistic.setDeliveryTime(null);
//        logistic.setTransportation(null);
//        logistic.setTransportPath(null);
//        logistic.setActualArrivalTime(null);
//        logistic.setExpectedArrivalTime(null);
//
//        fallbackList.add(order);
//        fallbackList.add(logistic);
//        return JSONResult.success(fallbackList,"200");
//    }
}
